<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="contain">
    <div>
      姓名：<input type="text" name="name" id="name">
    </div>
    <div>您输入的姓名为：<span id="show_name"></span></div>
  </div>
</body>
<script>
  let dataInfo = {
    _data: {
      name: '小猫'
    }
  }
  // view-model
  let box = document.getElementsByClassName('contain')[0];
  let showName = document.getElementById('show_name')

  window.onload = () => {
    showName.innerHTML = dataInfo._data.name;
  }

  Object.defineProperty(dataInfo, 'name', {
    get() {
      console.log('获取了name的值：', dataInfo._data.name)
      return dataInfo._data.name
    },
    set(val) {
      console.log('设置name的值：', val)
      dataInfo._data.name = val

      showName.innerHTML = dataInfo._data.name;
    }
  })

  box.addEventListener('input', (event) => {
    dataInfo[event.target.name] = event.target.value;
  })
</script>
</html>